/**
 * ...
 * @author Yog
 */

package cookinmaddy.model;

import cookinmaddy.model.Recipe;
import cookinmaddy.database.spod.DBRecipe;
import cookinmaddy.database.spod.DBRecipeManager;
import haxe.Serializer;
import php.Lib;

class RecipeManager 
{
	private static var TEMPLATE : String = "Recipeviewtemplate.mtt";

	public function new(){}
	
	public function getList() : String
	{
		trace("BEGIN");
		try
		{
			trace("get the Recipe list");
			var Recipelist = DBRecipe.manager.allRecipes(); // _root.getSubRecipes();
			trace("call templo");
			trace("END: RecipeView::getdisplay <br>");
			return haxe.Serializer.run(Recipelist);
		}
		catch (e : String)
		{
			trace("ERROR: " + e);
		}
		trace("END");
		return "";
	}
	
	public function update(value : Recipe) : String
	{
		trace("BEGIN");
		try
		{
			var db = DBRecipe.manager.get(value.ID);
			db.value = value;
			db.update();
		}
		catch (e : String)
		{
			trace("ERROR: " + e);
		}
		trace("END");
		return "OK";
	}
	
	public function add(value : Recipe) : String
	{
		trace("BEGIN");
		try
		{
			trace("in :");
			trace("- ID   : " + value.ID);
			trace("- Name : " + value.Name);
			var db : DBRecipe = new DBRecipe();
			db.value = value;
			trace("insert");
			db.insert();
			trace("serialize for return");
			var str : String =  haxe.Serializer.run(db.value);
			trace("return " + str);
			return str;
		}
		catch (e : String)
		{
			trace("ERROR : " + e);
		}
		trace("END");
		return "KO";
	}	
	
	public function delete(value : Recipe) : String
	{
		trace("BEGIN");
		try
		{
			var db = DBRecipe.manager.get(value.ID);
			db.value = value;
			db.delete();
			return cast value.ID;
		}
		catch (e : String)
		{
			trace("ERROR : " + e);
		}
		trace("END");
		return "KO";
	}		
}